package com.shj.em.biz.sell.controller;

import com.shj.em.biz.sell.entity.Ratio;
import com.shj.em.biz.sell.service.RatioService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.shj.em.common.annotation.Log;
import com.shj.em.base.support.ResMsg;

import com.shj.em.biz.sell.entity.Target;
import com.shj.em.biz.sell.service.TargetService;
import lombok.extern.slf4j.Slf4j;


/**
 * <p>
 * 年度销售目标 前端控制器
 * </p>
 *
 * @author Shen Huang Jian
 * @since 2018-07-15
 */
@RestController
@Slf4j
@RequestMapping("/conf")
public class ConfController{

    @Autowired
    TargetService targetService;

    @Autowired
    RatioService ratioService;

    @Log("加载年度销售目标")
    @GetMapping("/loadTarget/{year}")
    public ResMsg<Target> loadTarget(@PathVariable int year){
        Target target = targetService.getTargetByYear(year);
        if(target == null){
            target = new Target();
            target.setYear(year);
        }
        return ResMsg.ok(target);
    }

    @Log("编辑年度销售目标")
    @PostMapping("/saveTarget")
    public ResMsg<Long> saveTarget(@RequestBody Target target){
        if(target.getId() == null || target.getId().equals(0)){
            targetService.insert(target);
        }else{
            targetService.updateById(target);
        }
        Long id = targetService.getTargetByYear(target.getYear()).getId();
        return ResMsg.ok(id);
    }

    @Log("加载年度销售回款率目标")
    @GetMapping("/loadRatio/{year}")
    public ResMsg<Ratio> loadRatio(@PathVariable int year){
        Ratio ratio = ratioService.getRatioByYear(year);
        if(ratio == null){
            ratio = new Ratio();
            ratio.setYear(year);
        }
        return ResMsg.ok(ratio);
    }

    @Log("编辑年度销售回款率目标")
    @PostMapping("/saveRatio")
    public ResMsg<Long> saveRatio(@RequestBody Ratio ratio){
        if(ratio.getId() == null || ratio.getId().equals(0)){
            ratioService.insert(ratio);
        }else{
            ratioService.updateById(ratio);
        }
        Long id = ratioService.getRatioByYear(ratio.getYear()).getId();
        return ResMsg.ok(id);
    }

}

